
using Youz.Common.DBUtility;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Youz.Common.Helper;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// Pay_Records Data Class
    /// </summary>
    public partial class Pay_RecordsDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Pay_Records";

        public Pay_RecordsDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Pay_Records model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(@"insert into [Pay_Records](
                [PayType],[TradeNo],[TradeTime],[TradeName],[Payer],[Amount],[Status],[ParentID],[CreateTime],[ModifyTime]
                ) values (
                @PayType,@TradeNo,@TradeTime,@TradeName,@Payer,@Amount,@Status,@ParentID,@CreateTime,@ModifyTime
                )");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@PayType", (int)model.PayType),
                    new SqlParameter("@TradeNo", model.TradeNo),
                    new SqlParameter("@TradeTime", model.TradeTime),
                    new SqlParameter("@TradeName", model.TradeName),
                    new SqlParameter("@Payer", model.Payer),
                    new SqlParameter("@Amount", model.Amount),
                    new SqlParameter("@Status", (int)model.Status),
                    new SqlParameter("@ParentID", model.ParentID),
                    new SqlParameter("@CreateTime", DateTime.Now),
                    new SqlParameter("@ModifyTime", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Pay_Records model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Pay_Records] set ");
            strSql.Append("[Status]=@Status,[ModifyTime]=@ModifyTime");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@ID", model.ID),
                    new SqlParameter("@Status", (int)model.Status),
                    new SqlParameter("@ModifyTime", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateStatus(string tradeNo, PayRecordsStatus status)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Pay_Records] set ");
            strSql.Append("[Status]=@Status,[ModifyTime]=@ModifyTime");
            strSql.Append(" where [TradeNo]=@TradeNo");

            SqlParameter[] parameters = {
                    new SqlParameter("@TradeNo", tradeNo),
                    new SqlParameter("@Status", (int)status),
                    new SqlParameter("@ModifyTime", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int64 ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Pay_Records] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {
			
                new SqlParameter("@ID", ID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Pay_Records Get(Int64 ID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [Pay_Records] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {
			
                new SqlParameter("ID", ID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Pay_Records Get(DataRow row)
        {
            if (row == null) return null;

            Pay_Records model = new Pay_Records();


            if (row["ID"] != DBNull.Value)
            {
                model.ID = (Int64)row["ID"];
            }

            if (row["PayType"] != DBNull.Value)
            {
                model.PayType = (PaymentType)(Int32)row["PayType"];
            }

            if (row["TradeNo"] != DBNull.Value)
            {
                model.TradeNo = (String)row["TradeNo"];
            }

            if (row["TradeTime"] != DBNull.Value)
            {
                model.TradeTime = (DateTime)row["TradeTime"];
            }

            if (row["TradeName"] != DBNull.Value)
            {
                model.TradeName = (String)row["TradeName"];
            }

            if (row["Payer"] != DBNull.Value)
            {
                model.Payer = (String)row["Payer"];
            }

            if (row["Amount"] != DBNull.Value)
            {
                model.Amount = (Decimal)row["Amount"];
            }

            if (row["Status"] != DBNull.Value)
            {
                model.Status = (PayRecordsStatus)(Int32)row["Status"];
            }

            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }

            if (row["ModifyTime"] != DBNull.Value)
            {
                model.ModifyTime = (DateTime)row["ModifyTime"];
            }

            if (row["ParentID"] != DBNull.Value)
            {
                model.ParentID = (String)row["ParentID"];
            }

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetsWhere(string sqlWhere, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [Pay_Records] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString(), cmdParms);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Pay_Records] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Pay_Records] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Pay_Records]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by ID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法

        /// <summary>
        /// 将支付记录更新到对应的充值记录中
        /// </summary>
        /// <returns>充值号</returns>
        public string UpdateToRecharge()
        {
            var spRechargeID = new SqlParameter("@RechargeID", SqlDbType.BigInt);

            spRechargeID.Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedureNonQuery("up_UpdatePayToRecharge", new SqlParameter[] { spRechargeID });

            return spRechargeID.Value == null ? string.Empty : spRechargeID.Value.ToString();
        }

        public Pay_Records Get(string tradeNo)
        {
            var ds = GetsWhere("TradeNo=@TradeNo", new SqlParameter[] { new SqlParameter("@TradeNo", tradeNo) });

            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 生成ID
        /// </summary>
        /// <returns></returns>
        public string GenerateTradeNo()
        {
            var now = DateTime.Now;
            var id = (Int32)DbHelperSQL.GetSingle("select isnull(count(0),0) from [dbo].[Pay_Records] where DATEDIFF(ss, CreateTime, '" + now.ToString() + "')=0");
            var rand = new Random();

            return "6" + now.ToString("yyyyMMddHHmmss") + (id == 0 ? rand.Next(10, 800) : id + rand.Next(2, 20)).ToString().PadLeft(4, '0');
        }
    }
}


